﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TextTemplating;
using System.IO;
using System.CodeDom.Compiler;
using System.Runtime.Remoting.Messaging;

namespace MyToolkit.Com
{
    public class T4Output
    {
        public String TemplateContent { get; private set; }

        public List<String> Errors { get; private set; }

        public T4Output(string content)
        {
            TemplateContent = content;
            Errors = new List<string>();
        }

        public string Generation(object data)
        {
            CustomTextTemplatingEngineHost host = new CustomTextTemplatingEngineHost();
            host.TemplateFileValue = "test.tt";
            host.Session = new TextTemplatingSession();
            host.Session.Add("Data", data);
            Engine engine = new Engine();
            string result = engine.ProcessTemplate(TemplateContent, host);
            foreach (CompilerError error in host.Errors)
            {
                Errors.Add(error.ErrorText);
            }
            return result;
        }
    }
}
